//
//  BookDetailColCell.swift
//  WalkNovel
//
//  Created by Kevin on 2023/7/13.
//

import UIKit

class BookDetailColCell: UICollectionViewCell {
    
    lazy var disLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 12)
        label.textColor = .white
        label.backgroundColor = .init(named: "red_FE95B4")
        label.textAlignment = .center
        label.isHidden = true
        return label
    }()
    
    lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 20)
        label.textColor = .black
        label.numberOfLines = 10
        return label
    }()
    
    lazy var authorHeader: UIButton = {
        let btn = UIButton(type: .custom)
        btn.imageView?.contentMode = .scaleAspectFill
        btn.wn_cornerWithBorder(12)
        return btn
    }()
    
    lazy var authorLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 12)
        label.textColor = .init(named: "gray_666666")
        return label
    }()
    
    lazy var followCount: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 16)
        label.textColor = .black
        return label
    }()
    
    lazy var followLabel: UILabel = {
        let label = UILabel()
        label.text = "FOLLOW"
        label.font = .wn_font(name: .bold, size: 10)
        label.textColor = .black
        return label
    }()
    
    lazy var readCount: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 16)
        label.textColor = .black
        return label
    }()
    
    lazy var readLabel: UILabel = {
        let label = UILabel()
        label.text = "READ"
        label.font = .wn_font(name: .bold, size: 10)
        label.textColor = .black
        return label
    }()

    lazy var giftBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setBackgroundImage(UIImage(named: "book_gift"), for: .normal)
        btn.isHidden = true
        return btn
    }()
    
    lazy var scrollView: UIScrollView = {
        let scro = UIScrollView.init(frame: .zero)
        scro.showsHorizontalScrollIndicator = false
        scro.showsVerticalScrollIndicator = false
        scro.isHidden = true
        return scro
    }()
    
    lazy var giftCount: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 16)
        label.textColor = .black
        label.isHidden = true
        return label
    }()
    
    lazy var desTextView: IQTextView = {
        let view = IQTextView()
        view.font = .wn_font(name: .bold, size: 12)
        view.showsVerticalScrollIndicator = false
        view.textColor = .black
        view.linkTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.init(named: "red_FE95B4")!]
        view.isEditable = false
        view.isScrollEnabled = false
        view.backgroundColor = .white
        view.delegate = self
        view.textContainer.lineFragmentPadding = 0
        view.textContainerInset = .zero
        return view
    }()
    
    var showAllClosure: BoolClosure?
    
    var model: BookDetailModel? {
        didSet {
            if let m = model {
                nameLabel.text = m.name
                authorLabel.text = m.authorname
                authorHeader.wn_setupImg(m.icon, placeHolder: "placeholder_header")
                followCount.text = "\(m.follow)".wn_wan()
                readCount.text = "\(m.readings)".wn_wan()
                giftCount.text = "\(m.giftNum) Gifts"
                if m.showOpen {
                    desTextView.text = ""
                    desTextView.wn_appendLinkString(string: m.showDesAll ? m.desOpen : m.desClose)
                    desTextView.wn_appendLinkString(string: m.showDesAll ? "\n Less" : "\n..MORE", withURLString: m.showDesAll ? "https://Less" : "https://More")
                }else {
                    desTextView.text = m.des
                }
                giftArr = m.giftDetial ?? [GiftModel]()
                layoutIfNeeded()
            }
        }
    }
    
    var giftArr = [GiftModel]() {
        didSet {
            for item in scrollView.subviews {
                item.removeFromSuperview()
            }
            var contentW: CGFloat = 0.0
            guard giftArr.count > 0 else {
                scrollView.snp.updateConstraints { (make) in
                    make.width.equalTo(0)
                }
                return
            }
            let arr = giftArr.count > 2 ? giftArr.sample(size: 2) : giftArr
            for i in 0..<arr!.count {
                let btn = UIButton(type: .custom)
                btn.frame = CGRect(x: contentW, y: 0, width: 28, height: 28)
                btn.imageView?.contentMode = .scaleAspectFill
                btn.rx.tap.bind { [weak self] in
                    let vc = WNGiftListVC()
                    vc.baseId = self?.model?.bookId ?? ""
                    self?.viewContainingController?.navigationController?.pushViewController(vc, animated: true)
                }.disposed(by: rx.disposeBag)
                btn.wn_setupImg(arr?[i].cover, placeHolder: "gift_send")
                btn.wn_cornerWithBorder(14, 1, .init(named: "red_FE6390")!)
                contentW += 24
                scrollView.addSubview(btn)
            }
            contentW += 4
            if giftArr.count > 2 {
                let moreBtn = UIButton(type: .custom)
                moreBtn.frame = CGRect(x: contentW, y: 0, width: 28, height: 28)
                moreBtn.setImage(UIImage(named: "library_more"), for: .normal)
                moreBtn.rx.tap.bind { [weak self] in
                    let vc = WNGiftListVC()
                    vc.baseId = self?.model?.bookId ?? ""
                    self?.viewContainingController?.navigationController?.pushViewController(vc, animated: true)
                }.disposed(by: rx.disposeBag)
                contentW += 30
                scrollView.addSubview(moreBtn)
            }
            scrollView.snp.updateConstraints { (make) in
                make.width.equalTo(contentW)
            }
            scrollView.contentSize = CGSize(width: contentW, height: 0)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(disLabel)
        addSubview(nameLabel)
        addSubview(authorHeader)
        addSubview(authorLabel)
        addSubview(followCount)
        addSubview(followLabel)
        addSubview(readCount)
        addSubview(readLabel)
        addSubview(giftBtn)
        addSubview(giftCount)
        addSubview(scrollView)
        addSubview(desTextView)
        disLabel.snp.makeConstraints { (make) in
            make.top.equalTo(5)
            make.left.equalTo(12)
            make.width.equalTo(0)
            make.height.equalTo(0)
        }
        nameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(disLabel.snp.bottom).offset(5)
            make.left.equalTo(disLabel.snp.left)
            make.right.equalTo(-12)
        }
        authorHeader.snp.makeConstraints { (make) in
            make.top.equalTo(nameLabel.snp.bottom).offset(17)
            make.left.equalTo(nameLabel.snp.left)
            make.width.height.equalTo(24)
        }
        authorLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(authorHeader.snp.centerY)
            make.left.equalTo(authorHeader.snp.right).offset(4)
        }
        followCount.snp.makeConstraints { (make) in
            make.top.equalTo(authorHeader.snp.bottom).offset(12)
            make.left.equalTo(12)
        }
        followLabel.snp.makeConstraints { (make) in
            make.top.equalTo(followCount.snp.bottom).offset(14)
            make.left.equalTo(followCount.snp.left)
        }
        readCount.snp.makeConstraints { (make) in
            make.centerY.equalTo(followCount.snp.centerY)
            make.left.equalTo(scaleWidth(160))
        }
        readLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(followLabel.snp.centerY)
            make.left.equalTo(readCount.snp.left)
        }
        giftBtn.snp.makeConstraints { (make) in
            make.right.equalTo(-48)
            make.centerY.equalTo(authorHeader.snp.centerY).offset(-8)
            make.width.height.equalTo(32)
        }
        giftCount.snp.makeConstraints { (make) in
            make.centerX.equalTo(giftBtn.snp.centerX)
            make.centerY.equalTo(readCount.snp.centerY)
        }
        scrollView.snp.makeConstraints { (make) in
            make.centerX.equalTo(giftBtn.snp.centerX)
            make.centerY.equalTo(readLabel.snp.centerY)
            make.height.equalTo(32)
            make.width.equalTo(0)
        }
        desTextView.snp.makeConstraints { (make) in
            make.top.equalTo(followLabel.snp.bottom).offset(18)
            make.left.equalTo(12)
            make.right.equalTo(-12)
            make.bottom.equalTo(0)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension BookDetailColCell: UITextViewDelegate {
    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        if URL.absoluteString == "https://More" {
            showAllClosure?(true)
        }else if URL.absoluteString == "https://Less" {
            showAllClosure?(false)
        }else {
            
        }
        return false
    }
}
